package com.navi168.fwq21.friend;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import com.navi168.fwq21.R;
import com.navi168.fwq21.db.Sqlh;
import com.navi168.fwq21.friend.mod.Cheji;
import com.navi168.fwq21.friend.mod.Myfriend;
import com.navi168.fwq21.hyzl.PoiList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;

public class FriendView extends Activity {
	private EditText cymc;
	private EditText cjhm;
	private EditText cysj;
	private EditText email;
	private EditText addr;
	private Button okButton;
	private Button noButton;
	private Button usebuButton;
	private Button addButton;
	private int friendid;
	private Sqlh sqlh;
	private Bundle bundle;
	private ArrayList<Cheji> chejis = new ArrayList<Cheji>();
	private int numer = 0;
	private Hashtable<String, TableRow> trs = new Hashtable<String, TableRow>();
//	private ArrayList<EditText> ets = new ArrayList<EditText>();
	private Hashtable<String, EditText> editHashtable = new Hashtable<String, EditText>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.friend_d);
		sqlh = sqlh.getInstance(this);
		bundle = this.getIntent().getExtras();

		findviews();
		setListers();
		Log.i("mylog", "friendid--" + friendid);
		init_view();
	}

	private void init_view() {
		cymc.setText(bundle.getString("name"));
		// cjhm.setText(bundle.getString("cjh"));
		cysj.setText(bundle.getString("mob"));
		email.setText(bundle.getString("email"));
		addr.setText(bundle.getString("addr"));
		friendid = bundle.getInt("id");
		chejis = sqlh.getchejihao(bundle.getString("mob"));
		for (int i = 0; i < chejis.size(); i++) {
			final TableLayout tableLayout = (TableLayout) findViewById(R.id.cyxx_tablelayout);
			numer = tableLayout.getChildCount();

			TableRow tRow = new TableRow(FriendView.this);
			TableLayout.LayoutParams tlp = new TableLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			TableRow.LayoutParams trl = new TableRow.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			tRow.setLayoutParams(tlp);
			TextView tView = new TextView(FriendView.this);
			tView.setText("车机号 " + (numer - 4));
			EditText edit = new EditText(FriendView.this);
			edit.setText(chejis.get(i).getCheji());
			final Button delButton = new Button(FriendView.this);
			delButton.setId(numer);
			delButton.setText("del");
			delButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Log.i("mylog", delButton.getId() + "");
					tableLayout.removeView(trs.get(delButton.getId() + ""));
					editHashtable.remove(delButton.getId()+"");

				}
			});

			tView.setLayoutParams(trl);
			edit.setLayoutParams(trl);
			delButton.setLayoutParams(trl);

			tRow.addView(tView);
			tRow.addView(edit);
			tRow.addView(delButton);
			trs.put(numer + "", tRow);
			editHashtable.put(numer + "", edit);
//			ets.add(edit);
			tableLayout.addView(tRow);
		}

	}

	private void setListers() {
		usebuButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle bundle2 = new Bundle();
				bundle2.putString("query_key", addr.getText().toString());
				Intent intent = new Intent(FriendView.this, PoiList.class);
				intent.putExtras(bundle2);
				startActivity(intent);
			}
		});
		noButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FriendView.this.finish();

			}
		});
		okButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Myfriend mf = new Myfriend();
				mf.setAdress(addr.getText().toString());
//				mf.setFriendcar(cjhm.getText().toString());
				mf.setFriendemail(email.getText().toString());
				mf.setFriendid(friendid);
				mf.setFriendmob(cysj.getText().toString());
				mf.setFriendname(cymc.getText().toString());
				

				int a = sqlh.updateMyfriend(mf);
				
				ArrayList<Cheji> chejis2 = new ArrayList<Cheji>();
				if (editHashtable.size() > 0) {
					/*for (int i = 0; i < ets.size(); i++) {
						Cheji cj = new Cheji();
						cj.setId(chejis.get(i).getId());
						cj.setCheji(ets.get(i).getText().toString()
								.trim());
						cj.setMobile(cysj.getText().toString()
								.trim());
						chejis2.add(cj);
					}*/
					Enumeration   eunm = editHashtable.keys();
					while (eunm.hasMoreElements()) {
						Cheji cj = new Cheji();
						String key = (String) eunm.nextElement();
						cj.setCheji(editHashtable.get(key).getText().toString());
						cj.setMobile(cysj.getText().toString()
								.trim());
						chejis2.add(cj);
						
					}
					

					int flag3 = sqlh.del(cysj.getText().toString()
							.trim());
					int flag2 = sqlh.saveChejis(chejis2);
				}
				
				
				
				
				if (a > 0) {
					showToast("好友已更新！");
					Intent intent = new Intent(FriendView.this, FriendAct.class);
					startActivity(intent);
					FriendView.this.finish();
				} else {
					showToast("更新未成功，请稍后再试！");
				}

			}
		});

		addButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				final TableLayout tableLayout = (TableLayout) findViewById(R.id.cyxx_tablelayout);
				numer = tableLayout.getChildCount();

				TableRow tRow = new TableRow(FriendView.this);
				TableLayout.LayoutParams tlp = new TableLayout.LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
				TableRow.LayoutParams trl = new TableRow.LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				tRow.setLayoutParams(tlp);
				TextView tView = new TextView(FriendView.this);
				tView.setText("车机号 " + (numer - 3));
				EditText edit = new EditText(FriendView.this);
				final Button delButton = new Button(FriendView.this);
				delButton.setId(numer);
				delButton.setText("del");
				delButton.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Log.i("mylog", delButton.getId() + "");
						tableLayout.removeView(trs.get(delButton.getId() + ""));
						editHashtable.remove(delButton.getId()+"");

					}
				});

				tView.setLayoutParams(trl);
				edit.setLayoutParams(trl);
				delButton.setLayoutParams(trl);

				tRow.addView(tView);
				tRow.addView(edit);
				tRow.addView(delButton);
				trs.put(numer + "", tRow);
				editHashtable.put(numer + "", edit);
//				ets.add(edit);
				tableLayout.addView(tRow);

			}
		});

	}

	private void findviews() {
		cymc = (EditText) findViewById(R.id.cymc);
		// cjhm = (EditText) findViewById(R.id.cjhm);
		cysj = (EditText) findViewById(R.id.cysj);
		email = (EditText) findViewById(R.id.email);
		addr = (EditText) findViewById(R.id.addre);
		okButton = (Button) findViewById(R.id.save);
		noButton = (Button) findViewById(R.id.cance);
		usebuButton = (Button) findViewById(R.id.lookbutton);
		addButton = (Button) findViewById(R.id.addbutton);

	}

	private void showToast(String showString) {
		Toast.makeText(getApplicationContext(), showString, Toast.LENGTH_SHORT)
				.show();
	}
}
